import {
  queryWarnList, queryHistoryWarnList, queryWarnInfo, actionWarnClose, actionWarnExport,
} from '../services/warn';


export default {
  namespace: 'warn',

  state: {
    resdata: {
      list: [],
      pagination: {},
    },
    detaildata:{},
    dependentHistoryWarn:{},
    loading: {
      reslist: true,
      model_submit:false,
      detailinfo:true,
    },
  },

  effects: {
    // 获取当前告警信息
    *fetch_warn_list({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { reslist: true },
      });
      const response = yield call(queryWarnList, payload);
      response && (
        yield put({
          type: 'appendData',
          payload: response.data,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { reslist: false },
      });
    },

    // 获取历史告警信息
    *fetch_history_warnlist({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { reslist: true },
      });
      const response = yield call(queryHistoryWarnList, payload);
      response && (
        yield put({
          type: 'appendData',
          payload: response.data,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { reslist: false },
      });
    },

    // 获取关联的历史告警信息
    *fetch_dependenthistory_warnlist({ payload }, { call, put }) {
      const response = yield call(queryHistoryWarnList, payload);
      response && (
        yield put({
          type: 'appendDependentHistoryWarn',
          payload: response.data,
        })
      );
    },


    // 关闭告警
    *action_warn_close({ payload }, { call, put }) {
      const response = yield call(actionWarnClose, payload);
    },
    // 文件导出
    *action_warn_export({ payload }, { call, put }) {
      const response = yield call(actionWarnExport, payload);
    },

    // 获取单个详情
    *fetch_warn_info({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :true },
      });
      const response = yield call(queryWarnInfo, payload);
      response && (
        yield put({
          type: 'appendDataDetail',
          payload:response.data ,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :false },
      });
    },



  },

  reducers: {
    appendData(state, action) {
      return {
        ...state,
        resdata: action.payload,
      };
    },
    appendDependentHistoryWarn(state, action) {
      return {
        ...state,
        dependentHistoryWarn:action.payload,
      };
    },


    changeLoading(state, action) {
      return {
        ...state,
        loading: {
          ...state.loading,
          ...action.payload,
        },
      };
    },
    appendDataDetail(state, action) {
      return {
        ...state,
        detaildata: action.payload,
      };
    },

    appendClearState(state, action) {
      return {
        ...state,
        resdata:{},
        detaildata:{},
        dependentHistoryWarn:{},
        log:{}
      };
    },

  },
};
